#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：2.asyn结合requests完成爬虫任务.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-12-04 15:50
'''
import asyncio
import requests
from functools import partial # partial的作用是固定函数的某些参数，从而生成一个新的函数，偏函数
from bs4 import BeautifulSoup

url = "https://movie.douban.com/top250?start={}&filter="

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}


loop = asyncio.get_event_loop()

async def get_movie_info(page):
    response = await  loop.run_in_executor(None, partial(requests.get, url.format(page), headers=headers))
    soup = BeautifulSoup(response.text, "lxml")
    div_list = soup.find_all("div", class_="hd")
    for title in div_list:
        print(title.get_text())

if __name__ == '__main__':
    tasks = [loop.create_task(get_movie_info(page)) for page in range(0, 10)]
    loop.run_until_complete(asyncio.wait(tasks))
